/*
    This file is a part of ficus language project.
    See ficus/LICENSE for the licensing terms
*/

// jsonpp.fx example converted into a test

from UTest import *
import Json

TEST("json.parse", fun() {
    val js_data = "
    // sample json from https://ru.wikipedia.org/wiki/JSON
    {
        \"firstName\": \"Иван\",
        \"lastName\": \"Иванов\",
        \"address\": {
            \"streetAddress\": \"Московское ш., 101, кв.101\",
            \"city\": \"Ленинград\",
            \"postalCode\": 101101
        },
        \"phoneNumbers\": [
            \"812 123-1234\",
            \"916 123-4567\",
        ]
    }"
    val js = Json.parse_string("", js_data)
    match js {
    | Json.Map(m) =>
        match m.assoc_opt("address") {
        | Some(Json.Map(m)) =>
            EXPECT_EQ(m.assoc("city"), Json.Str("Ленинград"))
            EXPECT_EQ(m.assoc("postalCode"), Json.Int(101101i64))
        | _ => throw TestFailure("'address' is not found or is not a map")
        }
        match m.assoc_opt("phoneNumbers") {
        | Some(Json.Seq(s)) => EXPECT_EQ(s[1], Json.Str("916 123-4567"))
        | _ => throw TestFailure("'phoneNumbers' is not found or is not a sequence")
        }
    | _ => throw TestFailure("top-level json value is not a map")
    }
    EXPECT_EQ(`string(js)`,
"{
   \"firstName\": \"Иван\",
   \"lastName\": \"Иванов\",
   \"address\": {
      \"streetAddress\": \"Московское ш., 101, кв.101\",
      \"city\": \"Ленинград\",
      \"postalCode\": 101101
   },
   \"phoneNumbers\": [ \"812 123-1234\", \"916 123-4567\" ]
}")
})

TEST("json.pprint", fun() {

val rng = RNG(123u64)
val s_list_list = [for i <- 0:10 {
    val n = rng.uniform(0, 99)
    Json.Commented(f"#{i}", Json.Seq([for j <- 0:n {
        Json.Int(int64(rng.uniform(0, 99999)))}]))
    }]

val sample_js = Json.Commented(
    "small Json pretty-printing example",
    Json.Map([
    ("ain't it cool?", Json.Bool(true)),
    ("pi", Json.Commented("the famous constant", Json.Real(3.1415926))),
    ("a little array of arrays", Json.Commented("demonstrates compact representation of scalar arrays",
        Json.Seq(s_list_list))),
    ("greeting", Json.Commented("'hello' in Chinese", Json.Str("你好"))),
     ]))

val pprinted = string(sample_js)
EXPECT_EQ(`pprinted`,
"// small Json pretty-printing example
{
   \"ain't it cool?\": true,
   // the famous constant
   \"pi\": 3.1415926,
   // demonstrates compact representation of scalar arrays
   \"a little array of arrays\": [
      // #0
      [ 57889, 14964, 59715, 99018, 29021, 34935, 57451, 81727, 9511, 25844, 1646,
         29950, 4993, 34932, 18436, 85679, 23225, 4465, 39585, 99372, 38108, 53830,
         18850, 47743, 2091, 66977, 15408, 91937, 93135, 77826, 60018, 55193, 80332,
         73518, 34313, 52156, 690, 56812, 48126, 90583, 2179, 29234, 70629, 63510,
         59305, 49875, 19945, 48865, 13693, 91552, 76151, 7227, 40071, 21975, 88634,
         161, 51819, 85613, 92749, 42357, 60539, 87434, 78910, 35369, 5705, 91176,
         2839, 8877, 78326, 22464, 26345, 91345, 15497, 47311, 61897, 50260, 35332,
         97673, 42461, 66384, 18341, 93851, 48132, 76772, 61392, 86796, 78800, 96682,
         96376 ],
      // #1
      [ 78805, 10435, 3886, 11822, 22962, 82645, 62892, 87969, 60509, 10773, 62090,
         43519, 26008, 38393, 76780, 13016, 5577, 61317, 44822, 83703, 27462, 55533,
         39697, 73046, 70045, 80059, 33738, 57045, 48369, 13854, 43151, 57012, 27519,
         90397, 14582, 74197, 40155, 40005, 89066, 49721, 20450, 16466, 72454, 78129,
         99633, 7256, 17257, 62127, 99442, 45985, 64417, 63604, 54009, 87567, 81664,
         85271, 79397, 2829 ],
      // #2
      [ 31243, 2263, 36921, 22542, 50391, 15770, 89847, 45584, 20282, 71025, 30044,
         66051, 59156 ],
      // #3
      [ 94815, 35189, 25738, 37805, 70515, 29638, 34963, 29243, 52923, 67161, 72857,
         52393, 84810, 33090, 84609, 71326, 45229, 78197, 12546, 83548, 61726, 24296,
         97420, 82713, 82706, 9976, 45621, 82691, 48238, 69427, 5918, 58493, 33282,
         84019, 70794, 77420, 6268, 18097, 53375, 67295, 97151, 42977, 75677, 37960,
         66397, 30776, 1224 ],
      // #4
      [ 17533, 92701, 24230, 42254, 87534, 14889, 4928, 52080, 67473, 33754, 55152,
         32203, 55623, 23857, 55352, 27062, 40713, 24543, 48460, 35858, 36833, 61994,
         88309, 23626, 59860, 30271, 9710, 7668, 91566, 49475, 86313, 2449, 25391,
         28719, 22393, 60065, 81274, 18955, 76455, 75774, 1049, 11584, 34319, 74495,
         82080, 88422, 68854, 64426, 11140, 62075, 21647, 43859, 93405, 74458, 96960,
         71720, 52889 ],
      // #5
      [ 87316, 28176, 72042, 64715, 14311, 77929, 45401, 53892, 40587, 28051, 19345,
         85361, 51914, 11989, 93361, 57405, 5792, 55179, 73378, 30385, 66182, 24612,
         82527, 96705, 61887, 12737, 9657, 32100, 5678, 54563, 12682, 77079, 72293,
         51446, 13857, 74478, 31438, 57032, 98922, 40015, 169, 971, 42177, 92065,
         3900, 64306, 50869, 65141, 3920, 57583, 63829, 54328, 54029, 6729, 29153,
         59258, 49257, 79887, 75579, 47365, 89113, 67359, 58480 ],
      // #6
      [ 47492, 7309, 3847, 71600 ],
      // #7
      [ 75772, 31635, 59099, 62078, 10097, 70900, 3287, 88533, 23227, 92365, 75546,
         71614, 667, 831, 7321, 63967, 45111, 5597, 87030, 53538, 57102, 84480, 48388,
         17926, 4078, 54307, 34165, 70483, 55754, 52091, 53589, 29171, 58098, 48134,
         95450, 48517, 24561, 22746, 43239, 31388, 54261, 38363, 12849, 16942, 28026,
         25803, 22428, 85957, 87481, 51286, 32942, 21989, 87446, 82429, 73593, 59351,
         8024, 81163, 40523, 55463, 76526, 84028, 94045, 72349, 25733, 28471, 79591,
         31268, 63075, 36429, 75188, 40502, 86468, 39208, 53737, 27825, 29069, 10701,
         807, 93876, 45809, 31624, 63668, 76527, 17817, 52526, 55505, 32023, 2909,
         40287, 69981, 9863, 75836, 2582, 86853, 37591 ],
      // #8
      [ 97032, 88090, 8662, 9622, 47679, 25621, 82440 ],
      // #9
      [ 2222, 60423, 96026, 5950, 65306, 19501, 13739, 61042, 28123, 36330, 22714,
         33118, 59373, 95323, 63837, 55814, 27833, 81021, 48013, 47719, 53954, 57283,
         63477, 54342, 13817, 95024, 30185, 18379, 94862, 9439, 43652, 62413, 61894,
         60872, 25319, 55954, 13436, 62914, 37057, 28495, 2862, 76481, 81091, 48441,
         41362, 33754, 85498, 2759, 96589, 47109, 5597, 45997, 8069, 23574, 49211,
         14668, 83469, 20008, 65969, 8562, 10827, 27741, 98250, 77781, 70781, 88331,
         31652, 7708, 9885, 27696, 96168, 71098, 6901, 21093, 78088, 75214, 26173,
         81167, 39362, 40681, 21910, 39911, 9539, 20777, 58855, 37316, 60855, 91603,
         19777, 60031, 53394, 39581, 73633 ]
   ],
   // 'hello' in Chinese
   \"greeting\": \"你好\"
}")
})
